<?php
class SearchedArticleListWidget extends CWidget
{
    public $keyword = '';
    public $page_size = 10;
    public $content_limit = 50;

    public function run()
    {
        $this->page_size = intval($this->page_size);
        $this->content_limit = intval($this->content_limit);
        
        $criteria = new CDbCriteria();
        $criteria->with = array(
            'localeArticle' => array(
                'condition' => 'locale=:locale',
                'params' => array(':locale' => Yii::app()->language)
            ),
        );
        $criteria->compare('t.status',Article::STATUS_ACTIVE);
        $criteria->order = 't.create_time DESC, t.id DESC';
        if ($this->keyword!='')
        {
            $criteria->addCondition("((localeArticle.title LIKE '% {$this->keyword} %'
                    OR localeArticle.title = '{$this->keyword}' OR localeArticle.title LIKE '{$this->keyword} %' OR localeArticle.title LIKE '% {$this->keyword}')
                OR
                    (localeArticle.content LIKE '% {$this->keyword} %'
                        OR localeArticle.content = '{$this->keyword}'
                        OR localeArticle.content LIKE '{$this->keyword} %'
                        OR localeArticle.content LIKE '% {$this->keyword}'
                        OR localeArticle.content LIKE '%>{$this->keyword}<%'
                        OR localeArticle.content LIKE '%>{$this->keyword} %'
                        OR localeArticle.content LIKE '% {$this->keyword}<%')
            )");   
        }
        
        $countArticle = Article::model()->count($criteria);
        $paging = new CPagination($countArticle);
        $paging->pageSize = $this->page_size;
        $paging->applyLimit($criteria);
        
        $models = Article::model()->findAll($criteria);
        $this->render('article-list',array('models'=>$models,'paging'=>$paging));
    }
}
